# -*- coding: utf-8 -*-
"""
Created on Tue Aug 16 09:47:47 2022

@author: wulong
"""
import numpy as np
from casadi import *
from Basic_paras import *

# In[1] Fast EMPC 2 model
# States
def ode_ies_f2(x_f2, xs, x_f1, x_f3, us, u_f1, u_f2, u_f3, uz, ud):
    dxdt = SX.zeros(Nx_f2)
    
    C_soc = xs[0]
    C_sot = xs[1]
    C_stc = xs[2]
    C_sth = xs[3]
    tbr = xs[4]
    
    If = x_f1[0]
    Gh2 = x_f1[1]
    pO2 = x_f1[2]
    pH2O = x_f1[3]
    pH2 = x_f1[4]
    vcap = x_f1[5]
    Iba = x_f1[6]
    
    Pmtf = x_f2[0]
    tabf = x_f2[1]
    tabw = x_f2[2]
    tabt = x_f2[3]
    tewm = x_f2[4]
    tre = x_f2[5]
    
    tc = x_f3[0]
    tcs = x_f3[1]
    tcwm = x_f3[2]
    te = x_f3[3]
    tes = x_f3[4]
    
    Gab = us[0]
    Gec = us[1]
    Gstu = us[2]
    
    Gff = u_f1[0]
    Pbar = u_f1[1]
    
    Gfm = u_f2
    
    Nec = u_f3
    
    zfc = uz[0]
    zma = uz[1]
    zec = uz[2]
    zst = uz[3]
    
    ta = ud[0]
    Sra = ud[1]
    Pd = ud[2]
    Qother = ud[3]
    
    # real ST input with binary
    Gst = zst*Gstu + (zst - 1)*Gstu # ST
    mstc = C_sot*Mst
    
    # ST in the return side
    msth = Mst - mstc
    tsth = C_sth/(msth + eps)
    thp = zst*tre + (1 - zst)*tsth
    
    # PIP in the rerurn side
    Gsl = Gab + Gec + Gst
    trec = (Gsl*tre - Gst*thp)/(Gab + Gec + eps)
    
    # MA
    tab = zma*(tab0 + tabf + tabw + tabt)
    
    # EC
    tec = zec*(2*tewm - trec)
    
    # PIP in the supply side 1
    tslc = (Gab*tab + Gec*tec)/(Gab + Gec + eps)
    
    # ST in the supply side
    tstc = C_stc/(mstc + eps)
    tcp = zst*(tstc) + (1 - zst)*tslc
    
    # PIP in the supply side 2
    tsl = (Gab*tab + Gec*tec + Gst*tcp)/(Gsl + eps)
    
    # BR
    Q_the = Qsl0*(tbr - tsl)/delta_twa0*(Gsl/Gsl0)**0.6
    tre_cal = tsl + Q_the/(Gsl*Cw)
    
    dxdt[0] = zma*((kma1*Gfm**2 + kma2*Gfm + kma3 - Pmtf)/tau_mtf)
    dxdt[1] = zma*((kma4*Pmtf - tabf)/tau_abf)
    dxdt[2] = zma*((kma5*Gab + kma6 - tabw)/tau_abw)
    dxdt[3] = zma*((kma7*trec + kma8 - tabt)/tau_abt)
    dxdt[4] = zec*((Cw*Gec*(trec - tec) + alfaw*Aeo*(tes - tewm))/(Cw*Mew))
    dxdt[5] = (tre_cal - tre)/tau_ahu
    
    return dxdt

# Output
def out_ies_f2(x_f2, xs, x_f1, x_f3, us, u_f1, u_f2, u_f3, uz, ud):
    y_alg = MX.zeros(Ny_f2)
    
    C_soc = xs[0]
    C_sot = xs[1]
    C_stc = xs[2]
    C_sth = xs[3]
    tbr = xs[4]
    
    If = x_f1[0]
    Gh2 = x_f1[1]
    pO2 = x_f1[2]
    pH2O = x_f1[3]
    pH2 = x_f1[4]
    vcap = x_f1[5]
    Iba = x_f1[6]
    
    Pmtf = x_f2[0]
    tabf = x_f2[1]
    tabw = x_f2[2]
    tabt = x_f2[3]
    tewm = x_f2[4]
    tre = x_f2[5]
    
    tc = x_f3[0]
    tcs = x_f3[1]
    tcwm = x_f3[2]
    te = x_f3[3]
    tes = x_f3[4]
    
    Gab = us[0]
    Gec = us[1]
    Gstu = us[2]
    
    Gff = u_f1[0]
    Pbar = u_f1[1]
    
    Gfm = u_f2
    
    Nec = u_f3
    
    zfc = uz[0]
    zma = uz[1]
    zec = uz[2]
    zst = uz[3]
    
    ta = ud[0]
    Sra = ud[1]
    Pd = ud[2]
    Qother = ud[3]
    
    # PV
    I_pv_max = I_max_0*Sra/Sra_pv_0*(1 + c_pv1*(ta - T_pv_0))
    V_pv_max = V_max_0*log(exp(1) + c_pv2*(Sra - Sra_pv_0))*(1 - c_pv3*(ta - T_pv_0))
    Ppv = npp*nsp*I_pv_max*V_pv_max/1000
        
    # FC
    eta_a = afc + bfc*log(If + eps)
    eta_c = -R0fc*T0fc/(2*F0)*log(1-If/IL + eps)
    eta_o = If*rfc
    V0 = N0fc*(E0fc + R0fc*T0fc/(2*F0)*log(pH2*sqrt(pO2)/(pH2O + eps) + eps))
    Vfc = V0 - eta_a - eta_c - eta_o
    Ifc = fu_d/(2*Kr)*Gh2
    Pfc = zfc*(Vfc*Ifc/1000)
    
    # PIP in the rerurn side
    Gall = Gab + Gec + Gstu
        
    # MA
    Pmt = zma*(Pmt0 + Pmtf)
    
    # EC
    pc = exp(21.3-2025.5/(248.94+tc))/1e6
    pe = exp(21.3-2025.5/(248.94+te))/1e6
    pr = pc/pe # Compressor
    etavl = 0.98-0.085*(pr**(1/kr)-1)
    etacp = 0.9085*exp(-0.06443*pr)-7.605*exp(-3.155*pr)
    Gr = etavl*Nec*roeg*Vcp
    wi = kr/(1e3*(kr-1))*pe*1e6/roeg*(pr**((kr-1)/kr)-1)
    Pcp = zec*(Gr*wi/etacp)
    
    # BR
    Hpmp = Spmp*Gall**2/(rhow*ge)
    eta_pmp = b0p*Gall**2 + b1p*Gall + b2p
    Ppmp = Gall*ge*Hpmp/(1e3*eta_pmp)
    
    # BA
    iba = Iba/npb
    Vba = nsb*(Em - vcap - R0b*iba)
    Pba = Vba*Iba/1000
    
    # Electric Power
    Psc = Pcp + Ppmp
    Psl = Ppv + Pfc + Pmt + Pba - Psc - Pd
    
    y_alg = Psl
    
    return y_alg

# In[2] Formulate discrete time dynamics {x_f2, xs, x_f1, x_f3, us, u_f1, u_f2, u_f3, uz, ud}
x_sym_f2 = SX.sym('x_f2', Nx_f2)
x_s_sym_f2 = SX.sym('x_s_f2', Nx_s)
x_f1_sym_f2 = SX.sym('x_f1_f2', Nx_f1)
x_f3_sym_f2 = SX.sym('x_f3_f2', Nx_f3)

u_s_sym_f2 = SX.sym('u_s_f2', Nuc_s)
u_f1_sym_f2 = SX.sym('u_f1_f2', Nuc_f1)
u_sym_f2 = SX.sym('u_f2', Nuc_f2)
u_f3_sym_f2 = SX.sym('u_f3_f2', Nuc_f3)

uz_sym_f2 = SX.sym('uz_f2', Nuz_h)
ud_sym_f2 = SX.sym('ud_f2', Nud_f2)

ode_sym_f2 = ode_ies_f2(x_sym_f2, x_s_sym_f2, x_f1_sym_f2, x_f3_sym_f2, 
                        u_s_sym_f2, u_f1_sym_f2, u_sym_f2, u_f3_sym_f2, 
                        uz_sym_f2, ud_sym_f2)

args_f2 = {'x': x_sym_f2, 'p': vertcat(x_s_sym_f2, x_f1_sym_f2, x_f3_sym_f2, 
                                       u_s_sym_f2, u_f1_sym_f2, u_sym_f2, u_f3_sym_f2, 
                                       uz_sym_f2, ud_sym_f2), 'ode': ode_sym_f2}

opts_f2 = {'tf': Delta_f, 'regularity_check': True}
I_ode_f2 = integrator('I_ode_f2', 'rk', args_f2, opts_f2)

